package com.sangu.dianxiu.utils;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.sangu.dianxiu.BaseApp;
import com.sangu.dianxiu.ICallBack;
import com.sangu.dianxiu.config.Constants;

import java.util.Map;

/**
 * 基础数据网络请求的接口的实现类， 使用volley框架进行数据请求
 * Created by sg2 on 2016/1/4.
 */
public class BaseDataManager {
    public RequestQueue requestQueue= Volley.newRequestQueue(BaseApp.getInstance());           //volley框架的请求队列
    private StringRequest request;
    private StringRequest getRequest(final Map<String, String> map,final ICallBack callBack,String url)
    {
        StringRequest stringRequest=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                callBack.onSuccess(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                callBack.onError(error);
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
        stringRequest.setShouldCache(true);
        stringRequest.setTag(BaseDataManager.class);
        return stringRequest;
    }
//获得服务器端口号
    public void getServerPort(Map<String, String> map, ICallBack callBack) {
        request=getRequest(map,callBack, Constants.URL_GET_SERVER_PORT);
        requestQueue.add(request);
    }

//获得城市数据
    public void getCity(Map<String, String> map, ICallBack callBack) {
        request=getRequest(map,callBack, Constants.URL_GET_CITY);
        requestQueue.add(request);
    }

//获得区县数据
    public void getCounty(Map<String, String> map, ICallBack callBack) {
        request=getRequest(map,callBack, Constants.URL_GET_COUNTY);
        requestQueue.add(request);
    }

//获得用户评价短语
    public void getUserCommentPhrase(Map<String, String> map, ICallBack callBack) {
        request=getRequest(map,callBack, Constants.URL_GET_USER_COMMENT_PHRASE);
        requestQueue.add(request);
    }


    public void getCancelOrderPhrase(Map<String, String> map, ICallBack callBack) {
        request=getRequest(map,callBack, Constants.URL_GET_CANCEL_REASON_PHRASE);
        requestQueue.add(request);
    }

//获得首页banner
    public void getHomeAdv(Map<String, String> map, ICallBack callBack) {
        request=getRequest(map,callBack, Constants.URL_GET_HOME_ADV);
        requestQueue.add(request);
    }

//上传师傅反馈
    public void uploadRepairerFeedback(Map<String, String> map, ICallBack callBack) {
        request=getRequest(map,callBack, Constants.URL_UPLOAD_REPAIRER_FEEDBACK);
        requestQueue.add(request);
    }

//上传用户反馈
    public void uploadUserFeedback(Map<String, String> map, ICallBack callBack) {
        request=getRequest(map,callBack, Constants.URL_UPLOAD_USER_FEEDBACK);
        requestQueue.add(request);
    }

//上传文件
    public void uploadFile(Map<String, String> map, ICallBack callBack) {
        request=getRequest(map,callBack, Constants.URL_UPLOAD_FILE);
        requestQueue.add(request);
    }
}
